<template>
<div class="login"> 
    <div class="box-login">
      <el-card class="box-card">
      <div class="card-and-title">
        <h3>CRM后台管理系统</h3>
      </div>
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm"  class="demo-ruleForm">
      <el-form-item prop="pass">
          <el-input type="password" v-model="ruleForm.checkPass" placeholder="请输入您的账户">
            <el-button slot="prepend" icon="el-icon-user"></el-button>
          </el-input>
      </el-form-item>
      <el-form-item prop="checkPass">
          <el-input type="password" v-model="ruleForm.checkPass" placeholder="请输入密码">
            <el-button slot="prepend" icon="el-icon-lock"></el-button>
          </el-input>
      </el-form-item>
      <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
      </el-form-item>
      <el-form-item>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
      </el-form>
    </el-card>
    </div>
    </div>
</template>

<script>
  export default {
    data() {
      var checkAge = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('年龄不能为空'));
        }
        setTimeout(() => {
          if (!Number.isInteger(value)) {
            callback(new Error('请输入数字值'));
          } else {
            if (value < 18) {
              callback(new Error('必须年满18岁'));
            } else {
              callback();
            }
          }
        }, 1000);
      };
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.ruleForm.checkPass !== '') {
            this.$refs.ruleForm.validateField('checkPass');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.ruleForm.pass) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      return {
        ruleForm: {
          pass: '',
          checkPass: '',
          age: ''
        },
        rules: {
          pass: [
            { validator: validatePass, trigger: 'blur' }
          ],
          checkPass: [
            { validator: validatePass2, trigger: 'blur' }
          ],
          age: [
            { validator: checkAge, trigger: 'blur' }
          ]
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            alert('submit!');
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>
<style lang="scss" scoped>
.login{
    position: relative;
    width: 100%;
    height: 100%;
    background: url(../assets/bus_tu4.jpg) no-repeat;
    background-size: 100%;
}
.box-login{
  position: absolute;
    left: 50%;
    top: 50%;
    width: 400px;
    margin-top: -200px;
    margin-left: -150px;
    // height: 300px;
    // margin: -200px 0 0 -250px;
    border-radius: 5px;
    background: hsla(0,0%,100%,.3);
    overflow: hidden;
}
.el-form-item{
  margin-bottom: 12px;
}
.card-and-title{
    font-weight: 400;
    color: #1f2f3d;
    text-align: center;
    line-height: 56px;
    h3{
        padding: 0;
        margin: 0;
    }
}
.el-button{
  width: 100%;
}
</style>